package com.springboot.cloud.sysadmin.organization.rest;

import cn.hutool.core.util.StrUtil;
import com.alibaba.csp.sentinel.util.StringUtil;
import com.springboot.cloud.common.core.entity.vo.Result;
import com.springboot.cloud.common.core.util.TreeNodeForVueUtile;
import com.springboot.cloud.common.core.utils.StringUtils;
import com.springboot.cloud.sysadmin.organization.entity.form.DeptForm;
import com.springboot.cloud.sysadmin.organization.entity.form.DeptQueryForm;
import com.springboot.cloud.sysadmin.organization.entity.param.DeptQueryParam;
import com.springboot.cloud.sysadmin.organization.entity.po.Dept;
import com.springboot.cloud.sysadmin.organization.service.IDeptService;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * Created by ZengJian on 2021/2/3 0003 上午 9:22
 */
@RestController
@RequestMapping("/dept")
@Api(tags = "部门")
@Slf4j
public class DeptController {
    @Autowired
    private   IDeptService deptService;

    @ApiOperation(value = "新增部门", notes = "新增一个部门")
    @ApiImplicitParam(name = "deptForm", value = "新增部门form表单", required = true, dataType = "DeptForm")
    @PostMapping
    public Result add(@Valid @RequestBody DeptForm deptForm) {
        log.debug("deptForm:{}", deptForm);
        if (deptService.add(deptForm.toPo(Dept.class))) {
            return Result.success("添加部门成功");
        } else {
            return Result.error("同一父目录下不能有重复名称");
        }

    }

    @ApiOperation(value = "删除部门", notes = "根据url的id来指定删除对象")
    @DeleteMapping
    public Result delete(@Valid @RequestBody Set<String> ids) {
        if (StringUtils.isEmpty(ids)) {
            return Result.error("没选中要删除的部门");
        } else if (deptService.delete(ids)) {
            return Result.success("删除部门成功");
        }
        return Result.error("没选中要删除的部门");
    }

    @ApiOperation(value = "修改部门", notes = "修改指定部门信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "部门ID", required = true, dataType = "string"),
            @ApiImplicitParam(name = "deptForm", value = "部门实体", required = true, dataType = "DeptForm")
    })
    @PutMapping(value = "/{id}")
    public Result update(@PathVariable String id, @Valid @RequestBody DeptForm deptForm) {
        if (StrUtil.isBlank(id)) {
            return Result.error("id不可为空");
        }
        Dept dept = deptForm.toPo(Dept.class);
        dept.setId(id);
        if (deptService.update(dept)) {
            return Result.success("修改部门成功");
        }
        return Result.error("更新失败");
    }

    @ApiOperation(value = "获取部门", notes = "获取指定部门信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "部门ID", required = true, dataType = "string")
    @GetMapping(value = "/{id}")
    public Result get(@PathVariable String id) {
        if (StrUtil.isBlank(id)) {
            return Result.error("请选择部门");
        }
        return Result.success(deptService.get(id));
    }

    @ApiOperation(value = "查询部门", notes = "根据条件查询部门信息，简单查询")
    @ApiImplicitParam(paramType = "query", name = "name", value = "部门名称", required = true, dataType = "string")
    @ApiResponses(
            @ApiResponse(code = 200, message = "处理成功", response = Result.class)
    )
    @GetMapping
    public Result query(@RequestParam String name) {
        log.debug("query with name:{}", name);
        DeptQueryParam deptQueryParam = new DeptQueryParam();
        deptQueryParam.setName(name);
        return Result.success(deptService.query(deptQueryParam));
    }

    @ApiOperation(value = "搜索部门", notes = "根据条件查询部门信息")
    @ApiImplicitParam(name = "deptQueryForm", value = "部门查询参数", required = true, dataType = "DeptQueryForm")
    @ApiResponses(
            @ApiResponse(code = 200, message = "处理成功", response = Result.class)
    )
    @PostMapping(value = "/conditions")
    public Result search(@Valid @RequestBody DeptQueryForm deptQueryForm) {
        if (deptQueryForm.getCreatedTime() != null && !deptQueryForm.getCreatedTime().isEmpty()) {
            deptQueryForm.setCreatedTimeStart(deptQueryForm.getCreatedTime().get(0));
            deptQueryForm.setCreatedTimeEnd(deptQueryForm.getCreatedTime().get(1));
        }
        log.debug("search with deptQueryForm:{}", deptQueryForm);
        List<Dept> list = new ArrayList<>();
        list = deptService.query(deptQueryForm.toParam(DeptQueryParam.class));
        List<TreeNodeForVueUtile> tree = new ArrayList<>();
        for (Dept e : list) {
            tree.add(new TreeNodeForVueUtile(e, e.getName(), e.getId(), e.getParentId()));
        }
        List<TreeNodeForVueUtile> result = TreeNodeForVueUtile.getTreeResult(tree);
        return Result.success(result);
    }

    @ApiOperation(value = "搜索部门", notes = "根据条件查询部门信息")
    @ApiImplicitParam(name = "deptQueryForm", value = "部门查询参数", required = true, dataType = "DeptQueryForm")
    @ApiResponses(
            @ApiResponse(code = 200, message = "处理成功", response = Result.class)
    )
    @PostMapping(value = "/searchDept")
    public Result searchDept(@Valid @RequestBody DeptQueryForm deptQueryForm) {
        if (deptQueryForm.getCreatedTime() != null && !deptQueryForm.getCreatedTime().isEmpty()) {
            deptQueryForm.setCreatedTimeStart(deptQueryForm.getCreatedTime().get(0));
            deptQueryForm.setCreatedTimeEnd(deptQueryForm.getCreatedTime().get(1));
        }
        log.debug("search with deptQueryForm:{}", deptQueryForm);
        return Result.success(deptService.query(deptQueryForm.toParam(DeptQueryParam.class)));
    }

    @ApiOperation(value = "根据父id查询部门", notes = "根据父id查询部门列表")
    @ApiImplicitParam(paramType = "path", name = "id", value = "部门父ID", required = true, dataType = "string")
    @GetMapping(value = "/parent/{id}")
    public Result search(@PathVariable String id) {
        if (StrUtil.isBlank(id)) {
            return Result.error("parentId不可为空");
        }
        return Result.success(deptService.queryByParentId(id));

    }

    @ApiOperation(value = "获取所有部门", notes = "获取所有部门")
    @GetMapping(value = "/all")
    public Result get() {
        return Result.success(deptService.getAll());
    }

}
